<?php
namespace app\api\controller;

use think\Controller;
use app\api\model\UserModel as User;
use app\api\model\TokenModel as Token;
use app\api\model\SmsModel;

class Login extends Base
{
    public $user;
    public $userToken;
 	public $sms;
    public function __construct()
    {
        parent::__construct();
        $this->user = new User();
        $this->userToken = new Token();
      	$this->sms = new SmsModel();
    }

    public function login(){
         if($this->request->isPost()){
             $data = input();
             $user_name = $data['username'];
             $password = $data['password'];
             $user_info = $this->user->where(array("u_nickname"=>$user_name))->whereOr(array("u_phone"=>$user_name))->find();
             if($user_info){
                 if($user_info['u_state'] == 0){
                     $this->error("用户已冻结",'','-1');
                 }else{
                     if($user_info['u_pwd'] == md5(md5($password))){
                         $this->uid = $user_info['uid'];
                       	 $token = md5($user_name.time());
                         $info = $this->userToken->where(array("uid"=>$this->uid))->find();
                         if(!empty($info)){
                             $res = $this->userToken->where(array("uid"=>$this->uid))->update(array("token"=>$token));
                         }else{
                             $arr = array();
                             $arr['uid'] = $user_info['uid'];
                             $arr['token'] = $token;
                             $arr['createtime'] = time();
                             $res = $this->userToken->insert($arr);
                         }
                         if($res){
                             $this->success("登录成功",array("token"=>$token,"uid"=>$user_info['uid']),'1');
                         }else{
                             $this->success("登录失败",'','-1');
                         }
                     }else{
                         $this->error("密码输入有误",'','-1');
                     }
                 }
             }else{
                 $this->error("用户不存在",'','-1');
             }
         } else {
             $this->error("提交信息错误",'','-1');
         }
     }
  //忘记密码
    public function retrieve_password()
    {
        if($this->request->isPost()){
            $account = input("phone",'');
            $sms_code = input("sms_verification",'');
            $password = input('affirmpassword','');
            $password1 = input("newpassword",'');
            $user_info = $this->user->getInfo(array("u_phone"=>$account));
            if(empty($user_info)){
                AjaxReturn('-1',"用户不存在");
            }
            if($password != $password1){
                AjaxReturn('-1',"两次密码输入不一致");
            }
            $code = $this->sms->where(array("mobile"=>$account,"times"=>0))->order("createtime desc")->find();
            if(empty($code)){
                AjaxReturn('-1',"验证码已过期");
            }else{
                if($code['code'] != $sms_code){
                    AjaxReturn('-1',"验证码不正确");
                }
            }
            $res = $this->user->where(array("u_phone"=>$account))->update(array("u_pwd"=>md5(md5($password))));
            if($res){
                $this->sms->where(array('mobile'=>$account))->order("createtime desc")->update(array("times"=>1));
                AjaxReturn('1',"修改成功");
            }else{
                AjaxReturn('-1',"修改失败");
            }
        }else{
            AjaxReturn('-1',"提交信息错误");
        }
    }
}